<template>
  <div>
    <Header></Header>
    <div class="p-40 button-nav">
      <div class="" style="width: 800px;margin: auto">
        <div class="pt-10 pb-10 pl-20 pr-20 bg-grey flex font-14">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>考生登录</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="bg-shadow mt-20 p-40 bg-white">
          <el-form :model="form" label-width="120px"
                   ref="formRef" :rules="rules"
                   style="width: 50%;margin: auto;">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" placeholder="请输入姓名"/>
            </el-form-item>
            <el-form-item label="身份证号" prop="id_card">
              <el-input v-model="form.id_card" placeholder="请输入身份证号"/>
            </el-form-item>
<!--            <el-form-item label="密码" prop="password">-->
<!--              <el-input v-model="form.password" placeholder="请输入密码" type="password"/>-->
<!--            </el-form-item>-->
            <el-form-item>
              <el-button type="primary" @click="onSubmit">登录</el-button>
              <el-button @click="goBack">返回</el-button>
            </el-form-item>
          </el-form>
          <div class="text-center">
            <el-button type="primary" link @click="register">考生注册</el-button>
            <el-button type="" link @click="goForget">找回密码</el-button>
          </div>
        </div>
      </div>
    </div>
    <Footer></Footer>
  </div>
</template>

<script>
import Header from "@/components/Header";
import Footer from "@/components/Footer";
import { login } from "@/api/api";

const rules = {
  username: [
    { required: true, message: '请输入姓名', trigger: 'blur' }
  ],
  id_card: [
    { required: true, message: '请输入身份证号', trigger: 'blur' }
  ],
  // password: [
  //   { required: true, message: '请输入密码', trigger: 'blur' }
  // ]
}

export default {
  name: "Login",
  components: {
    Header, Footer
  },
  data() {
    return {
      form: {
        username: '',
        password: '',
        id_card: ''
      },
      rules: rules
    }
  },
  mounted() {
    const _this = this
    const user = JSON.parse(localStorage.getItem('xx-token'))
    if (user) {
      _this.$router.push('/')
    }
  },
  methods: {
    onSubmit() {
      const _this = this
      this.$refs.formRef.validate(async (valid) => {
        if (!valid) {
          this.$message.error('请填写或选择所有必填项')
          return false
        }
        // operation code
        const data = await login(this.form)
        if (data) {
          this.$message.success('登录成功')
          localStorage.setItem('xx-token', data.id_card)
          setTimeout(()=>{
            _this.$router.push('/')
            // window.location.reload('')
          }, 1000)
        }
      })
    },
    goBack() {
      this.$router.back()
    },
    register() {
      this.$router.push('/register')
    },
    goForget() {
      this.$router.push('/user/password')
    }
  }
}
</script>

<style scoped>

</style>
